package org.taohuawu.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.taohuawu.pojo.Item;
import org.taohuawu.pojo.Order;
import org.taohuawu.pojo.User;
import org.taohuawu.web.util.JsonResult;

import java.util.Arrays;
import java.util.List;

@RestController
public class RibbonController {
    @Autowired
    private RestTemplate restTemplate;

    // http://localhost:3001/item-service/35
    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFallback") // 指定降级方法的名称
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) {
        //return restTemplate.getForObject("http://localhost:8001/item/{1}", JsonResult.class, orderId);

        //这里服务器路径用 service-id 代替，ribbon 会向服务的多台集群服务器分发请求
        return restTemplate.getForObject("http://item-service/item/{1}", JsonResult.class, orderId);
    }

    // http://localhost:3001/item-service/decreaseNumber
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFallback")
    public JsonResult decreaseNumber(@RequestBody List<Item> list) {
        return restTemplate.postForObject("http://item-service/item/decreaseNumber", list, JsonResult.class);
    }

    // http://localhost:3001/user-service/9
    @GetMapping("/user-service/{userId}")
    @HystrixCommand(fallbackMethod = "getUserFallback")
    public JsonResult<User> getUser(@PathVariable Integer userId) {
        return restTemplate.getForObject("http://user-service/user/{1}", JsonResult.class, userId);
    }

    // http://localhost:3001/user-service/8/score?&score=99
    @GetMapping("/user-service/{userId}/score")
    @HystrixCommand(fallbackMethod = "addScoreFallback")
    public JsonResult addScore(@PathVariable Integer userId, Integer score) {
        return restTemplate.getForObject("http://user-service/user/{1}/score", JsonResult.class, score);
    }

    // http://localhost:3001/order-service/202107221156
    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFallback")
    public JsonResult<Order> getOrder(@PathVariable String orderId) {
        return restTemplate.getForObject("http://order-service/order/{1}", JsonResult.class, orderId);
    }

    // http://localhost:3001/order-service
    @GetMapping("/order-service")
    @HystrixCommand(fallbackMethod = "addOrderFallback")
    public JsonResult addOrder() {
        return restTemplate.getForObject("http://order-service/order/", JsonResult.class);
    }

    // 降级方法的参数和返回值需要和原始方法一致，方法名任意
    private JsonResult<List<Item>> getItemsFallback(String orderId) {
        return JsonResult.err("获取商品列表失败！");
    }


    private JsonResult decreaseNumberFallback(List<Item> list) {
        return JsonResult.err("更新商品库存失败！");
    }

    private JsonResult<User> getUserFallback(Integer userId) {
        return JsonResult.err("获取用户信息失败！");
    }

    private JsonResult addScoreFallback(Integer userId, Integer score) {
        return JsonResult.err("用户增加积分失败！");
    }

    private JsonResult<Order> getOrderFallback(String orderId) {
        return JsonResult.err("获取订单失败！");
    }

    private JsonResult addOrderFallback() {
        return JsonResult.err("新增订单失败！");
    }
}
